Skip to content

Fix integer overflow in Vulkan multiply_integers#18681

Open
lucylq wants to merge 1 commit intomainfrom
security27
Open

Fix integer overflow in Vulkan multiply_integers#18681
lucylq wants to merge 1 commit intomainfrom
security27

Conversation

@lucylq
Copy link
Copy Markdown
Contributor

@lucylq lucylq commented Apr 2, 2026

Replace std::accumulate with c10::mul_overflows to check for overflow at each multiplication.

Prevents undersized GPU buffer allocations from attacker-controlled tensor dimensions in PTE files.

This PR was authored with the assistance of Claude.

Test plan

CI

cc @SS-JIA @manuelcandales @digantdesai @cbilgin

@pytorch-bot pytorch-bot bot added the module: vulkan Issues related to the Vulkan delegate and code under backends/vulkan/ label Apr 2, 2026
@pytorch-bot
Copy link
Copy Markdown

pytorch-bot bot commented Apr 2, 2026

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/pytorch/executorch/18681

Note: Links to docs will display an error until the docs builds have been completed.

❌ 8 New Failures, 2 Unrelated Failures

As of commit 7762227 with merge base 3d2c853 (image):

NEW FAILURES - The following jobs have failed:

FLAKY - The following job failed but was likely due to flakiness present on trunk:

BROKEN TRUNK - The following job failed but was present on the merge base:

👉 Rebase onto the `viable/strict` branch to avoid these failures

This comment was automatically generated by Dr. CI and updates every 15 minutes.

@meta-cla meta-cla bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Apr 2, 2026
@github-actions
Copy link
Copy Markdown

github-actions bot commented Apr 2, 2026

This PR needs a release notes: label

If your change should be included in the release notes (i.e. would users of this library care about this change?), please use a label starting with release notes:. This helps us keep track and include your important work in the next release notes.

To add a label, you can comment to pytorchbot, for example
@pytorchbot label "release notes: none"

For more information, see
https://github.com/pytorch/pytorch/wiki/PyTorch-AutoLabel-Bot#why-categorize-for-release-notes-and-how-does-it-work.

@lucylq lucylq marked this pull request as ready for review April 2, 2026 21:52
@lucylq lucylq requested a review from SS-JIA as a code owner April 2, 2026 21:52
Copilot AI review requested due to automatic review settings April 2, 2026 21:52
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR hardens the Vulkan backend’s integer multiplication utility used for tensor size/numel calculations by adding per-step overflow detection, preventing undersized GPU buffer allocations when tensor dimensions are attacker-controlled (e.g., from PTE files).

Changes:

  • Add c10::mul_overflows-based overflow checking to multiply_integers (iterator overload).
  • Update the container overload to delegate to the iterator overload.
  • Add the necessary c10/util/safe_numerics.h include.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Copilot AI review requested due to automatic review settings April 6, 2026 18:06
Replace std::accumulate with std::multiplies<>() with an explicit loop
using safe_multiply_int64() that pre-checks for overflow before each
multiplication. Prevents undersized GPU buffer allocations from
attacker-controlled tensor dimensions in PTE files.

Addresses TOB-EXECUTORCH-27.

This PR was authored with the assistance of Claude.
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 1 out of 1 changed files in this pull request and generated 1 comment.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines 478 to +484
inline int64_t multiply_integers(Iter begin, Iter end) {
// std::accumulate infers return type from `init` type, so if the `init` type
// is not large enough to hold the result, computation can overflow. We use
// `int64_t` here to avoid this.
return std::accumulate(
begin, end, static_cast<int64_t>(1), std::multiplies<>());
int64_t result = 1;
for (Iter it = begin; it != end; ++it) {
VK_CHECK_COND(
!c10::mul_overflows(result, static_cast<int64_t>(*it), &result),
"Integer overflow in multiply_integers");
}
Copy link

Copilot AI Apr 6, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

multiply_integers() now checks for int64_t multiplication overflow, but it still permits negative factors. Since tensor sizes in PTE schema are signed (schema/program.fbs:108), a malicious file could supply negative dims; the product can become negative and then be implicitly converted to size_t/uint32_t at call sites, resulting in huge allocations or other incorrect behavior. Consider explicitly rejecting negative factors (and possibly also validating result fits the intended unsigned destination type) before returning.

Copilot uses AI. Check for mistakes.
Copy link
Copy Markdown
Contributor

@SS-JIA SS-JIA left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall the intent of the change is sound. However, I want to avoid bringing a c10 dependency into Vulkan backend, if possible.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. module: vulkan Issues related to the Vulkan delegate and code under backends/vulkan/ security-fix

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants